import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionListener;

import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.recognition.impl.StopRecognition;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.log4j.Layout;

import recommend.Recommender;
import recommend.RecommenderImpl;
import segmenter.ChineseSegmenter;
import segmenter.ChineseSegmenterImpl;
import util.FileHandler;
import util.FileHandlerImpl;
import vo.StockInfo;

public class IOGUI extends JPanel {
	JButton previousButton = new JButton("返回上一步");
	JTextArea inputText = new JTextArea("请填入检索的语句...",10,25);
	JButton startButton = new JButton("开始检索");
	FlowLayout layoutM = new FlowLayout(FlowLayout.CENTER);
	String input;//用户输入的文本
	File filePath;//文件路径
	JList<String> resultList = new JList<String>();
	JScrollPane scrollpanel = new JScrollPane(resultList);
	CardLayout cl;//接受总布局管理器
	JPanel mainPanel;//接受主面板
	public IOGUI(CardLayout cl,JPanel mainPanel) {
		//初始化总布局管理器和主面板
		this.cl = cl;
		this.mainPanel = mainPanel;
		//设置布局管理器
		setLayout(layoutM);
		//添加返回按钮	
		previousButton.addMouseListener(new MouseEventListener());
		add(previousButton);
		//添加开始检索按钮
		startButton.addMouseListener(new MouseEventListener());
		add(startButton);
		//添加用户输入文本区域
		inputText.setLineWrap(true);
		add(inputText);	
		//添加结果列表
		resultList.addMouseListener(new MouseEventListener());
		scrollpanel.setPreferredSize(new Dimension(450, 450));
		add(scrollpanel);
	}
	public void setFilePath(File path) {
		filePath = path;
	}
	private class MouseEventListener extends MouseAdapter{
		public void mouseClicked(MouseEvent event) {
			if(event.getSource()==startButton) {
				input = inputText.getText();
				if(input == null) {
					System.out.println("输入内容不能为空!");
					return;
				}
				//在输入不为空的情况下处理文本
				int[] searchResult = Proccess(input,filePath);
				String[] searchStringResult =new String[10];
				StockInfo[] stocks = new FileHandlerImpl().getStockInfoFromFile(filePath.getAbsolutePath());
				for(int i=0;i<10;i++) {
					searchStringResult[i] =stocks[searchResult[i]].getID()
										+"		"+stocks[searchResult[i]].getTITLE()
										+"		"+ stocks[searchResult[i]].getAUTHOR()
										+"		"+ stocks[searchResult[i]].getDATE()
										+"		"+stocks[searchResult[i]].getLASTUPDATE()
										+"		"+ stocks[searchResult[i]].getCONTENT()
										+"		"+stocks[searchResult[i]].getANSWERAUTHOR()
										+"		"+ stocks[searchResult[i]].getANSWER();
				}
				resultList.setListData(searchStringResult);
			}
			else if(event.getSource()==resultList) {
				String temp = resultList.getSelectedValue();
				List<Term> terms = new ChineseSegmenterImpl().getWordsFromInput(temp);
//				String[] segmentResult = new String[terms.size()];
//				for(int i=0;i<terms.size();i++) {
//					segmentResult[i] = terms.get(i).getName();
//				}
				StockInfo[] stocks = new FileHandlerImpl().getStockInfoFromFile(filePath.getAbsolutePath());
				String[] keyWords = new RecommenderImpl().calculateTF_IDF(terms, stocks);
//				//test-code-0
//					for(String x:keyWords) {
//						System.out.println(x);
//					}
//				//test-code-1
				try {
					new GenerateWordCloud(keyWords).setWordCloud2NewWindow();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(event.getSource()==previousButton) {
				cl.show(mainPanel,"File Card");
			}
		}
	}
	
	private int[] Proccess(String input,File filepath) {
		//测试代码--开始
//		System.err.println(input);
//		System.out.println(filepath.getAbsolutePath());
		//测试代码--结束
		//读取文件中的信息结构体数组
		FileHandler file = new FileHandlerImpl();
		StockInfo[] stocks = file.getStockInfoFromFile(filepath.getAbsolutePath());
		//对input进行分词
		List<Term> terms = new ChineseSegmenterImpl().getWordsFromInput(input);
		//将分词结果转换成String数组
		String[] inputSegmentResult = new String[terms.size()];//输入文本的分词结果
		for (int j = 0; j < terms.size(); j++) {
			inputSegmentResult[j] = terms.get(j).getName();
		}
		Recommender re = new RecommenderImpl();
		int[] searchResult= re.calculateMatrix(stocks, inputSegmentResult);
		return searchResult;
	}

}
